package trinidad;

/**
 * NeTrinidad
 */

import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.KeyStroke;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.event.MenuEvent;
import javax.swing.event.MenuListener;

public class NeTrinidadApp implements ActionListener, MenuListener {
	private static final long serialVersionUID = 1L;
	private static String windows = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
	   
	public JFrame winMain;
	private String opponent = "human";
	private int maxdepth = 2;
	private int overrun = 10000;
	private boolean reverseturns = false;
	private boolean humanfirst = true;
	private BattleField winPanel;
	private JButton winButton;
	private JMenuItem undoMenuItem;

	private String ip = "localhost";
	private String port= "4444";
	private boolean sieciowa = false;

	public NeTrinidadApp()
	{
		
		try {
			UIManager.setLookAndFeel(windows);
		} catch (UnsupportedLookAndFeelException exc) {
		} catch (IllegalAccessException exc) {
		} catch (ClassNotFoundException exc) {
		} catch (InstantiationException exc) {
		}
		
		winMain = new JFrame("Trynidad 2006");
		JMenuBar mainMenuBar = addMainMenuBar();
		winMain.setJMenuBar(mainMenuBar);
		
		winMain.setResizable(false);
		//new NewGameWindow(this);
		this.newGame();		
	}
	
	public void newGame()
	{
		if(winPanel != null) {
			this.winMain.remove(winPanel);
		}
		//to do wersji sieciowej
		//winPanel.ip = this.ip;
		//winPanel.sieciowa = this.sieciowa;
		winPanel = new BattleField(maxdepth, overrun, opponent, reverseturns, humanfirst, ip,sieciowa);
		
		winPanel.setBackground(Color.WHITE);
		winPanel.validate();
		winButton = new JButton("Tu stoje!");
		winButton.setVisible(false);
		winButton.setEnabled(false);
		winButton.addActionListener(new ActionListener() {
		    public void actionPerformed(ActionEvent e) {
		    	winPanel.imDone();
		    }
		});
		winPanel.join(BattleField.NEXTTURNBUTTON, winButton);
		Container con = winMain.getContentPane();
		con.setLayout(new BoxLayout(con,BoxLayout.Y_AXIS));
		con.add(winButton);
		con.add(winPanel);

		winMain.setSize(330,370);
		winMain.addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});
		winMain.setVisible(true);
	}
	
	public void setParameters(int maxdepth, int overrun, String opponent, boolean reverseturns, boolean humanfirst) {
		this.maxdepth = maxdepth;
		this.overrun = overrun;
		this.opponent = opponent;
		this.reverseturns = reverseturns;
		this.humanfirst = humanfirst;
	}
	
	public void setNetParams(String ip, String port, boolean b){
		this.ip = ip;
		this.port = port;
		this.sieciowa = b;
	}
	
	
	public static void main(String[] args) {
		new NeTrinidadApp();
	}
	
	protected JMenuBar addMainMenuBar() {
		JMenuBar mainMenuBar = new JMenuBar();
		JMenu mGame = new JMenu("Gra");
		mGame.setMnemonic('g');
		mGame.addMenuListener(this);
	
		JMenuItem menuItem = new JMenuItem("Nowa gra");
		menuItem.setActionCommand("Nowa gra");
		menuItem.addActionListener(this);
		menuItem.setMnemonic('n');
		menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,
				InputEvent.ALT_MASK));
		mGame.add(menuItem);

		menuItem = new JMenuItem("Restart gry");
		menuItem.setActionCommand("Restart gry");
		menuItem.addActionListener(this);
		menuItem.setMnemonic('r');
		menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_R,
				InputEvent.ALT_MASK));
		mGame.add(menuItem);
		
		mGame.addSeparator();
		
		undoMenuItem = new JMenuItem("Cofnij ruch");
		undoMenuItem.setActionCommand("Cofnij ruch");
		undoMenuItem.addActionListener(this);
		undoMenuItem.setMnemonic('c');
		undoMenuItem.setEnabled(false);
		undoMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,
				InputEvent.ALT_MASK));
		mGame.add(undoMenuItem);
		
		mGame.addSeparator();
		
		menuItem = new JMenuItem("Najlepsi gracze");
		menuItem.setActionCommand("Najlepsi gracze");
		menuItem.addActionListener(this);
		mGame.add(menuItem);
		
		mGame.addSeparator();

		menuItem = new JMenuItem("Wyjście");
		menuItem.setActionCommand("Wyjście");
		menuItem.addActionListener(this);
		menuItem.setMnemonic('w');
		menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_W,
				InputEvent.ALT_MASK));
		mGame.add(menuItem);

		mainMenuBar.add(mGame);

		JMenu mHelp = new JMenu("Pomoc");
		mHelp.setMnemonic('h');

		menuItem = new JMenuItem("Informacje");
		menuItem.setActionCommand("Informacje");
		menuItem.addActionListener(this);
		menuItem.setMnemonic('i');
		menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_I,
				InputEvent.ALT_MASK));
		mHelp.add(menuItem);

		/*menuItem = new JMenuItem("Autorzy");
		menuItem.setActionCommand("Autorzy");
		menuItem.addActionListener(this);
		menuItem.setMnemonic('a');
		mHelp.add(menuItem);
		 */
		mainMenuBar.add(mHelp);
		
		JMenu mNet = new JMenu("Gra sieciowa");
		mNet.setMnemonic('s');

		menuItem = new JMenuItem("Nowa gra sieciowa");
		menuItem.setActionCommand("net_new");
		menuItem.addActionListener(this);
		menuItem.setMnemonic('S');
		menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,
				InputEvent.ALT_MASK));
		mNet.add(menuItem);
		menuItem = new JMenuItem("Dolącz się");
		menuItem.setActionCommand("new_get");
		menuItem.addActionListener(this);
		menuItem.setMnemonic('d');
		menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_D,
				InputEvent.ALT_MASK));
		mNet.add(menuItem);
		mainMenuBar.add(mNet);
		
		return mainMenuBar;
	}

	public void actionPerformed(ActionEvent e) {
		String cmd = e.getActionCommand();
		
		if (cmd.equals("Nowa gra")) {
			new NewGameWindow(this);
			this.winMain.validate();
		} else if (cmd.equals("Restart gry")) {
			this.newGame();
		} else if (cmd.equals("Cofnij ruch")) {
			this.winPanel.undoState();
		} else if (cmd.equals("Najlepsi gracze")) {
			JOptionPane.showMessageDialog(null,(new BestResults()).toString(),"Lista najlepszych",JOptionPane.INFORMATION_MESSAGE);
		} else if (cmd.equals("Wyjście")) {
			System.exit(0);
		} else if (cmd.equals("Informacje")) {
			new GameRules(this.winMain);
			System.out.println("informacje");
		} else if (cmd.equals("Autorzy")) {
			System.out.println("autorzy");
		}else if (cmd.equals("net_new")) {
			new NewNetGame(this,true,true);
			//this.winMain.validate();;
		} else if (cmd.equals("new_get")) {
			new NewNetGame(this,false,false);
			//this.winMain.validate();;
		}
	}

	public void menuCanceled(MenuEvent arg0) {
		;
	}

	public void menuDeselected(MenuEvent arg0) {
		;
	}

	public void menuSelected(MenuEvent arg0) {
		if(this.winPanel.canUndoState()) {
			this.undoMenuItem.setEnabled(true);
		}
		else {
			this.undoMenuItem.setEnabled(false);
		}
	}
}
